/**
 * @file
 */
 
#ifndef WII_GI_C_MUTEX_H
#define WII_GI_C_MUTEX_H

#include <gccore.h>

#include "e_Status.h"

namespace wiigi
{
   class c_Mutex
   {
      public:
         
      /**
       * constructor
       */
      c_Mutex( bool Recursive = true )
      {
         LWP_MutexInit( &m_Muxtex, Recursive );
         
         return;
      }
      
      /**
       *destructor
       */
      ~c_Mutex()
      {
         LWP_MutexDestroy( m_Muxtex );
         
         return;
      }
      
      /**
       * lock
       */
      e_Status lock( void )
      {
         return ( LWP_MutexLock( m_Muxtex ) == 0 ) ? d_StatusOK : d_Failed;
      }
      
      /**
       * unlock
       */
      e_Status unlock( void )
      {
         return ( LWP_MutexUnlock( m_Muxtex ) == 0 ) ? d_StatusOK : d_Failed;
      }
      
      /**
       * small class to lock/release mutex
       */
      class Locker
      {
         public:
            Locker( c_Mutex& Mutex )
            :
               m_Muxtex( Mutex )
            {
               
               m_Muxtex.lock();
               
               return;
            }
            
            ~Locker()
            {
               m_Muxtex.unlock();
               
               return;
            }
            
         private:
            c_Mutex& m_Muxtex;
            
      };       
      
      private:

         mutex_t m_Muxtex; 
      
   };
   
}

#endif //WII_GI_C_MUTEX_H
